package com.atguigu.gmall.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.constant.RedisConst;
import com.atguigu.gmall.entity.GmallCorrelationData;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * 公共提取 RabbitMQ 发送消息
 */
@SuppressWarnings("all")
@Component
public class RabbitService {


    @Autowired
    private RabbitTemplate rabbitTemplate;//手机
    @Autowired
    private RedisTemplate redisTemplate;
    //发消息
    public boolean sendMessage(String exchange,String routingKey,Object message){
        //当交换机接收到不到消息的时候 作为回调参数进行重新发送
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //唯一主键
        correlationData.setId(UUID.randomUUID().toString());
        //交换机
        correlationData.setExchange(exchange);
        //路由Kye
        correlationData.setRoutingKey(routingKey);
        //消息体
        correlationData.setMessage(message);

        //手动转换Json
        //redis 保存 GmallCorrelationData  K：correlationData.getId()  V：GmallCorrelationData
        redisTemplate.opsForHash().put(RedisConst.REPLY_MESSAGE,
                correlationData.getId(),JSONObject.toJSONString(correlationData));

        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,message,correlationData);
        return true;
    }

    //发延迟消息
    public void sendDelayMessage(String exchange,String routingKey,Object msg,String time){
        //当交换机接收到不到消息的时候 作为回调参数进行重新发送
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //唯一主键
        correlationData.setId(UUID.randomUUID().toString());
        //交换机
        correlationData.setExchange(exchange);
        //路由Kye
        correlationData.setRoutingKey(routingKey);
        //消息体
        correlationData.setMessage(msg);
        //是否为延迟消息
        correlationData.setDelay(true);
        //延迟消息的时长
        //correlationData.setDelayTime();
        //手动转换Json
        //redis 保存 GmallCorrelationData  K：correlationData.getId()  V：GmallCorrelationData
        redisTemplate.opsForHash().put(RedisConst.REPLY_MESSAGE,
                correlationData.getId(),JSONObject.toJSONString(correlationData));

        //发延迟消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,(message) -> {
            //局部设置延迟时间
            message.getMessageProperties().setExpiration(time);
            System.out.println("发送的时间：" + new Date());
            return message;
        },correlationData);
    }


    //插件
    public void sendDelayMessagePlugins(String exchange,String routingKey,Object msg,int time){
        //当交换机接收到不到消息的时候 作为回调参数进行重新发送
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //唯一主键
        correlationData.setId(UUID.randomUUID().toString());
        //交换机
        correlationData.setExchange(exchange);
        //路由Kye
        correlationData.setRoutingKey(routingKey);
        //消息体
        correlationData.setMessage(msg);
        //是否为延迟消息
        correlationData.setDelay(true);
        //延迟消息的时长
        //correlationData.setDelayTime();
        //手动转换Json
        //redis 保存 GmallCorrelationData  K：correlationData.getId()  V：GmallCorrelationData
        redisTemplate.opsForHash().put(RedisConst.REPLY_MESSAGE,
                correlationData.getId(),JSONObject.toJSONString(correlationData));
        //发延迟消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,(message) -> {
            //局部设置延迟时间
            message.getMessageProperties().setDelay(time*1000);
            System.out.println("发送的时间：" + new Date());
            return message;
        },correlationData);
    }






    public static void main(String[] args) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();

        GmallCorrelationData correlationData = new GmallCorrelationData();
        //唯一主键
        correlationData.setId(UUID.randomUUID().toString());
        String string = objectMapper.writeValueAsString(correlationData);
        System.out.println(string);
        //{"future":{"done":false,"cancelled":false},"id":"912e7478-7bc8-4408-9bd6-1236430f94a7","returnedMessage":null,"message":null,"exchange":null,"routingKey":null,"retryCount":0,"delayTime":10,"delay":false}
    }

}
